package com.doitedu.mr.day05.index;

import com.google.inject.internal.cglib.transform.$ClassTransformer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @Date 2021/12/6
 * @Created by HANGGE
 * @Description TODO
 * hello-a.html	4
 * hello-c.html	6
 */
public class Index02 {
    static class  Index02Mapper extends Mapper<LongWritable ,Text, Text ,Text> {
        Text k = new Text() ;
        Text v = new Text() ;
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String[] split = line.split("-");
            String word = split[0];
            String fileNameAndCount = split[1];
            String[] split1 = fileNameAndCount.split("\\s+");
            fileNameAndCount =  split1[0]+","+split1[1];
           // fileNameAndCount.replaceAll("\\t" ,",") ;
            k.set(word);
            v.set(fileNameAndCount);
            context.write(k,v);
        }
    }
    static  class  Index02Reducer extends Reducer<Text ,Text ,Text , Text> {
       //<a.html	4, b.html   4 ,c.html	6>
        Text v = new Text() ;
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            ///String res = "" ;
            StringBuilder sb = new StringBuilder();
            for (Text value : values) {
                String fileAndCount = value.toString();
                sb.append(fileAndCount).append(" ");
             // res+=fileAndCount+",";
            }
            String res = sb.toString().trim();
            v.set(res);
            context.write(key , v);

        }
    }

    public static void main(String[] args) throws  Exception{

        // Job  统计文件中单词出现的次数
        Configuration conf = new Configuration();
        // 1  创建 JOB
        Job job = Job.getInstance(conf, "index02");
        // 1) Mapper 类
        job.setMapperClass(Index02.Index02Mapper.class);
        // 2) Reducer类
        job.setReducerClass(Index02Reducer.class);
        // 3)  当map端和reduce端的输出类型完全一致 可以省略
        /*  job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);*/
        // 4)  设置reduce输出的数据类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
       // job.setNumReduceTasks(2);
        // 5) 设置输入的数据路径  处理的是上会不的输出结果
        FileInputFormat.setInputPaths(job , new Path("E:\\mrdata\\index\\out01"));
        // 6) 设置输出的结果保存路径
        FileOutputFormat.setOutputPath(job, new Path("E:\\mrdata\\index\\out05"));
        // 2 提交工作
        // 等待执行完成 , 打印执行过
        job.waitForCompletion(true) ;
    }


}
